﻿using UnityEngine;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine.Events;
using System.Linq;

public class UIView : MonoBehaviour
{
    private GameObject _buttonPanel;
    private Button _provinceButton;
    private Button _cityButton;
    private Button _districtButton;

    private GameObject _scrollPanel;
    private ScrollViewScript _ScrollView;

    void Awake()
    {
        Init();
        print("_provinceButton:" + _provinceButton.name);
        print("_cityButton:" + _cityButton.name);
        print("_districtButton:" + _districtButton.name);
    }

    void Init()
    {
        _buttonPanel = transform.GetChild(0).gameObject;
        _provinceButton = _buttonPanel.transform.GetChild(0).GetComponent<Button>();
        _cityButton = _buttonPanel.transform.GetChild(1).GetComponent<Button>();
        _districtButton = _buttonPanel.transform.GetChild(2).GetComponent<Button>();
        _provinceButton.onClick.AddListener(ShowAllProvinces);
        _cityButton.onClick.AddListener(delegate()
        {
            ShowAllCitysByProvince(_provinceButton.GetComponentInChildren<Text>().text);
        });
        _districtButton.onClick.AddListener(delegate()
        {
            ShowDistrictsByCity(_provinceButton.GetComponentInChildren<Text>().text, _cityButton.GetComponentInChildren<Text>().text);
        });

        _scrollPanel = transform.GetChild(1).gameObject;
        _ScrollView = _scrollPanel.GetComponentInChildren<ScrollViewScript>();
        //_ScrollView.ComponentInit();
        _scrollPanel.SetActive(false);
    }

    //显示所有的省
    public void ShowAllProvinces()
    {
        Debug.Log(UIManager.GetInstance().datas.Keys.ToList<string>().Count);
        _scrollPanel.SetActive(true);
        _ScrollView.SetScrollContentValue(UIManager.GetInstance().datas.Keys.ToList<string>(), "province");
    }

    //根据省显示下面所有的市
    public void ShowAllCitysByProvince(string provinceName)
    {
        if (UIManager.GetInstance().datas.ContainsKey(provinceName))
        {
            _scrollPanel.SetActive(true);
            _ScrollView.SetScrollContentValue(UIManager.GetInstance().datas[provinceName].Keys.ToList<string>(), "city");
        }
        print(provinceName);
    }

    //根据市显示下面所有的区
    public void ShowDistrictsByCity(string provinceName, string cityName)
    {
        if (UIManager.GetInstance().datas.ContainsKey(provinceName) && UIManager.GetInstance().datas[provinceName].ContainsKey(cityName))
        {
            _scrollPanel.SetActive(true);
            _ScrollView.SetScrollContentValue(UIManager.GetInstance().datas[provinceName][cityName], "district");
        }
        print(cityName);
    }

    public void RefreshCityInfo(City currentCity)
    {
        _provinceButton.GetComponentInChildren<Text>().text = currentCity.province;
        _cityButton.GetComponentInChildren<Text>().text = currentCity.city;
        _districtButton.GetComponentInChildren<Text>().text = currentCity.district;
    }
}
